/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.dal;

import client.entity.CvCategory;
import client.entity.CvLocation;
import client.entity.Education;
import client.entity.EmploymentHistory;
import client.entity.Worker;
import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author mrlucky
 */
public class WorkerDAL {

    public WorkerDAL() {
    }

    public void insertWorker(Worker w) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.save(w);
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }

    public Worker loadWorkerByAccountID(int accID) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        Worker w = (Worker) ss.createQuery("from Worker w where w.account.id =" + accID).uniqueResult();
        ss.beginTransaction().commit();
        ss.close();
        return w;
    }

    public void updateWorker(Worker w) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.update(w);
        ss.beginTransaction().commit();
        ss.close();
    }

    public void insertEducation(ArrayList<Education> list) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        for (Education e : list) {
            ss.save(e);
        }
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public void deleteEducationByCvId(int cvId)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.createQuery("delete from Education e where e.cv.id ="+cvId).executeUpdate();
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }

    public void insertEmployHistory(ArrayList<EmploymentHistory> list) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        for (EmploymentHistory eh : list) {
            ss.save(eh);
        }
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public void deleteEmployHistoryByCvId(int cvId)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.createQuery("delete from EmploymentHistory eh where eh.cv.id ="+cvId).executeUpdate();
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public Worker loadWorkerById(int workerId)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        Worker w = (Worker) ss.createQuery("from Worker w where w.id ="+workerId).uniqueResult();
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
        return w;
    }

    
}
